package cn.rongdao.jrkabao.ui.cashadvance.addcard.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;
import cn.rongdao.jrkabao.widget.CardEditText;

/**
 * Created by jiangyongxing on 2017/6/2.
 * 描述：
 */

public class ShowShowOrWriteComponent extends LinearLayout {

    private TextView mTitle;
    private CardEditText mCardEditText;
    private TextView mShowRight;
    private TextView mShowContent;
    private ImageView ivBankIcon;
    private OnBankNumberLength mOnBankNumberLenght;

    public interface OnBankNumberLength {
        void onShowBankNameAndIcon(String bankNumber);

        void onBuried();

        void onDismissBankNameAndIcon();
    }

    public void setOnBankNumberLength(OnBankNumberLength onBankNumberLenght) {
        this.mOnBankNumberLenght = onBankNumberLenght;
    }

    public ShowShowOrWriteComponent(Context context) {
        super(context);
    }

    public ShowShowOrWriteComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ShowShowOrWriteComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTitle = findViewById(R.id.acssw_title);
        mCardEditText = findViewById(R.id.acssw_write_content_card_number);
        ivBankIcon = findViewById(R.id.acssw_iv_bank_icon);
        mShowContent = findViewById(R.id.lcssw_tv_content);
        mShowRight = findViewById(R.id.lcssw_tv_right);

        mCardEditText.addTextChangedListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                if (mOnBankNumberLenght != null) {
                    if (mCardEditText.getContentString().length() == 1) {
                        mOnBankNumberLenght.onBuried();
                    }
                    if (mCardEditText.getContentString().length() >= 15) {
                        mOnBankNumberLenght.onShowBankNameAndIcon(mCardEditText.getContentString());
                    } else if (mCardEditText.getContentString().length() < 15) {
                        mOnBankNumberLenght.onDismissBankNameAndIcon();
                    }
                }
            }
        });

        setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
    }

    public void setBankCardIcon(String icon) {
        mTitle.setVisibility(View.GONE);
        ivBankIcon.setVisibility(View.VISIBLE);
        FRImageLoad.loadImage(FRString.valueOf(icon), ivBankIcon, R.drawable.icon_no_distinguish_bank);
    }

    public void initContent(String title, String content) {
        mTitle.setText(title);
        mTitle.setVisibility(View.VISIBLE);
        ivBankIcon.setVisibility(View.GONE);
        mShowContent.setVisibility(View.VISIBLE);
        mCardEditText.setVisibility(View.GONE);
        mShowContent.setText(FRString.valueOf(content));
        mShowContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
        mShowContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_arrow_gray, 0);
        mShowContent.setTextColor(ContextCompat.getColor(getContext(), R.color.c999999));
    }

    public void setShowContent(String content) {
        mShowContent.setText(FRString.valueOf(content));
        mShowContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        mShowContent.setTextColor(ContextCompat.getColor(getContext(), R.color.c666666));
    }

    public void setRightCameraClickListener(OnClickListener onClickListener) {
        mShowRight.setVisibility(View.VISIBLE);
        mShowRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_camera, 0);
        mShowRight.setOnClickListener(onClickListener);
    }

    public void initCardEditText(String title, String hintContent) {
        mTitle.setText(title);
        ivBankIcon.setVisibility(View.GONE);
        mShowContent.setVisibility(View.GONE);
        SpannableString ss = new SpannableString(hintContent);
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(13, true);
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mCardEditText.setHint(new SpannedString(ss));
    }

    public void setCardEditTextBankCardTypeOn() {
        mCardEditText.setBankCardTypeOn();
    }

    public String getCardEditText() {
        return mCardEditText.getContentString();
    }

    public void setEditTextContent(String content) {
        mCardEditText.setText(FRString.valueOf(content));
        if (!FRString.isEmpty(content)) {
            mCardEditText.setSelection(content.length());
        }
    }

    public void initEditText(String title, String hint, int type) {
        mCardEditText.setInputType(type);
        initCardEditText(title, hint);
    }
}